package com.ruoyi.web.controller.myController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.ruoyi.common.config.lphCosConfig;
import com.ruoyi.system.domain.LiveAuthentication;
import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.system.mapper.LiveAuthenticationMapper;
import com.ruoyi.system.mapper.MyuserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class MyLiveAuthenticationController {
    @Autowired
    private LiveAuthenticationMapper liveAuthenticationMapper;
        @PostMapping("/saveLiveAuthentication")
    public Results saveLiveAuthentication(@RequestParam("uid") Integer uid,
                                          @RequestParam("age") Integer age,
                                          @RequestParam("level") String level,
                                          @RequestParam("picture") MultipartFile picture,
                                          @RequestParam("type") Integer type) {
        String originalFilename = picture.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (suffix.equals(".jpg") || suffix.equals(".png") || suffix.equals(".jpeg")||suffix.equals(".JPG") || suffix.equals(".PNG") || suffix.equals(".JPEG")) {
            COSClient cosClient = new lphCosConfig().cosClient();
            String url = null;
            String newfilename = UUID.randomUUID().toString() + suffix;
            ObjectMetadata objectMetadata = new ObjectMetadata();
            try {
                InputStream inputStream = picture.getInputStream();
                objectMetadata.setContentLength(inputStream.available());
                PutObjectRequest putObjectRequest1 = new PutObjectRequest(new lphCosConfig().getbucket(), newfilename, inputStream, objectMetadata);
                cosClient.putObject(putObjectRequest1);
                url = "https://" + new lphCosConfig().getbucket() + ".cos.ap-guangzhou.myqcloud.com" + "/" + newfilename;
                LiveAuthentication liveAuthentication = new LiveAuthentication();
                liveAuthentication.setUid(uid.longValue());
                liveAuthentication.setAge(age.longValue());
                liveAuthentication.setLevel(level);
                liveAuthentication.setPicture(url);
                liveAuthentication.setType(type.longValue());
                int insert = liveAuthenticationMapper.insertLiveAuthentication(liveAuthentication);
                return new Results(400, "成功", insert);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            return new Results(500,"类型不符合",null);
    }
    @GetMapping("/selectByUid/{uid}")
    public Results getUid(@PathVariable("uid") Integer uid){
        LiveAuthentication liveAuthentication=null;
        LambdaQueryWrapper<LiveAuthentication> liveAuthenticationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        liveAuthenticationLambdaQueryWrapper.eq(LiveAuthentication::getUid,uid);
        liveAuthentication = liveAuthenticationMapper.selectLiveAuthenticationByUid(uid.longValue());
        if(liveAuthentication==null){
            return new Results(404,"",null);
        }
        return new Results(400,"",null);
    }
}
